package com.lianrui.app.cart.service.impl;

import static com.lianrui.message.IBusinessMessage.BIS_GOODS_NOT_EXISTS;
import static com.lianrui.message.IBusinessMessage.BIS_OPERATE_FAILED;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianrui.app.cart.dao.ICartDao;
import com.lianrui.app.cart.domain.Cart;
import com.lianrui.app.cart.domain.CartItem;
import com.lianrui.app.cart.service.ICartService;
import com.lianrui.app.cart.vos.CartItemVO;
import com.lianrui.app.cart.vos.CartPage;
import com.lianrui.app.goods.dao.IProductDao;
import com.lianrui.app.goods.domain.Goods;
import com.lianrui.app.goods.domain.Product;
import com.lianrui.app.goods.service.IGoodsService;
import com.lianrui.commons.exception.BusinessException;
import com.lianrui.commons.tools.Functions;
import com.lianrui.commons.validate.InputValidate;
import com.lianrui.core.AbstractService;
import com.lianrui.core.IDao;
import com.lianrui.core.IDao.OperateType;
import com.lianrui.message.IBusinessMessage;

/**
 * 功能描述：
 * 
 * @author LJQ
 * 
 * <br>2016年1月7日 下午2:28:16
 */
@Service("cartService")
public class CartService extends AbstractService<CartPage, Cart, Integer> implements ICartService {
	
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory.getLogger(CartService.class);
	@Autowired
	IProductDao  productDao;
	@Autowired
	ICartDao cartDao;
	@Autowired
	IGoodsService goodsService;
	
	/**
	 * 根据会员获取购物车列表
	 * @param member_id : 会员id
	 * @return ： 购物车列表
	 * @throws BusinessException
	 */
	public List<CartItemVO> all(String member_id)throws BusinessException{
		List<CartItemVO> cartItemVOs = new LinkedList<CartItemVO>();
		
		List<CartItem> cartItems = cartDao.all(member_id);
		
		if(null != cartItems && cartItems.size() > 0){
			CartItemVO cartItemVO = null;
			for(CartItem o : cartItems){
				String img = Functions.getImage(o.getImage_file(), 1, o.getImage_default());
				o.setImage_default(img);
				
				if(StringUtils.isNotBlank(o.getSpecs())){
					o.setNameSpecs(StringUtils.defaultString(o.getName()) +"("+ o.getSpecs() +")" );
				}
				else{
					o.setNameSpecs(StringUtils.defaultString(o.getName()));
				}
				try {
					cartItemVO = new CartItemVO();
					BeanUtils.copyProperties(cartItemVO,o);
					cartItemVOs.add(cartItemVO);
				} catch (Exception e) {
					throw new  BusinessException(BIS_OPERATE_FAILED, getLogger());
				}
			}
		}
		
		return cartItemVOs;
	}
	
	
	/**
	 * 添加商品到购物车
	 */
	public void add(Cart cart) throws BusinessException {
		validate(OperateType.add, cart);
		
		Integer num = cart.getNum(); //放入购物车的商品数量
		Integer leinum = cart.getLeinum();//小类
		num = (null == num || num <= 0 ) ? 1 : num;
		cart.setNum(num);
		
		// 只有商标注册的才有小项
		Goods goods = goodsService.pure(cart.getProduct_id());
		if (null !=  goods) {
			String default_count_number  =  (String)goods.getDefault_count_number();//商品默认的小项
			String name  =  (String)goods.getName();
			if (!name.contains("商标注册")) { // 不包含商标注册的小项都设置为0
				leinum = 0;
			}
			else{
				leinum = Integer.parseInt(default_count_number);
			}
			
			Product  product = productDao.unique(cart.getProduct_id());
			Integer itemtype = 0;
			cart.setItemtype(itemtype);
			cart.setWeight(product.getWeight());
			cart.setLeinum(leinum);
			cart.setPrice(product.getPrice());
			cart.setName(product.getName());
			cart.setNum(1);
			for(int i = 0; i < num; i++){
				cartDao.add(cart);
			}
		}
		else{
			throw new  BusinessException(BIS_GOODS_NOT_EXISTS, getLogger());
		}
		
	}
	
	@Override
	public int validate(OperateType opType, final Cart cart) throws BusinessException {
		return new InputValidate(IBusinessMessage.BIS_MODEL_CART) {
			{
				noNull(cart);
				noNull(cart.getProduct_id(), IBusinessMessage.BIS_FIELD_PRODUCTID);
				noNull(cart.getMember_id(), IBusinessMessage.BIS_FIELD_MEMBERID);
			}
		}.validate();
	}
	
	@Override
	public IDao<CartPage, Cart, Integer> getDao() throws BusinessException {
		return cartDao;
	}

	@Override
	protected Logger getLogger() {
		return LOGGER;
	}
	
}
